package com.cloud;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.mcavallo.opencloud.Cloud;
import org.mcavallo.opencloud.Cloud.Case;
import org.mcavallo.opencloud.Tag;

import dao.TagDAO;
import dao.TagTypeDAO;
import domain.TagType;

public class GenerateWordCloud {

	private TagDAO dao;

	public void initUI() {
		
		final TagTypeDAO tagType = new TagTypeDAO();
		final JFrame frame = new JFrame(GenerateWordCloud.class.getSimpleName());
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		final JPanel combopanel = new JPanel();
		JPanel cloudpanel1 = new JPanel();
		final JComboBox Categories = new JComboBox();
		String type = new String();
		
		List<TagType> tagTypes = tagType.selectAllTagTypes();
			
		for (TagType t : tagTypes) {
			type = t.getTag_type();
			Categories.addItem(t);
		}
	
		Categories.setSelectedIndex(0);
		
		Categories.setAlignmentX(Component.CENTER_ALIGNMENT);
	
		JLabel cat = new JLabel(
				"<html> <p> Please select a type from the dropdown list</p> <br /></html>");
		combopanel.add(cat);
		combopanel.add(Categories);
		
		
		 frame.add(combopanel, BorderLayout.PAGE_START);
		 frame.add(cloudpanel1, BorderLayout.CENTER);
		 frame.setSize(1000, 800);
		 frame.setResizable(false);
		 frame.setVisible(true);
		
		
		Categories.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				final JPanel cloudpanel = new JPanel();
				final Cloud cloud = new Cloud();
				cloud.setTagCase(Case.PRESERVE_CASE);
				cloud.setMaxWeight(80d);
				cloud.setMinWeight(10d);
				
				TagType typeSelected = (TagType)Categories.getSelectedItem();
				//int tagID = tagType.selectTypeID(cat);
				final List<domain.Tag> tagsForType = dao.selectAllByType(typeSelected.getId());
				
				//cloud.setTagCase(ne);
				Random random = new Random();
				for (domain.Tag tag : tagsForType) {
					//for (int i = random.nextInt(80); i > 0; i--) {
						Tag t = new Tag(tag.getTag(), (double)tag.getCounter());
						t.setWeight((double)tag.getCounter());
						cloud.addTag(t);
						
						System.out.println(tag.getTag() +" "+ tag.getCounter());
					//}
				}

				System.out.println("Generating Cloud");
				for (Tag tag : cloud.tags()) {
					JLabel label = new JLabel(tag.getName());
					/*CustomJPanel two = new CustomJPanel(tag.getName());
					final String val = two.getS();
					JButton b2 = new JButton("Show Diff Table");
					two.add(b2);
					b2.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0) {
							new LargeWordDiffTable(val, dao, tagsForType);
							selected=val;
							//System.out.println(val);
						}
					});*/

					//System.out.println(tag.getName());
					/*--------------------------------------------------------CHANGE ME-----------------------------------------*/
					label.addMouseListener(new CustomMouseListener(tag.getName(), tagsForType, typeSelected.getId())); //CHANGE HARDCODE 1 LATER (NEEDS DD TYPE)
					/*--------------------------------------------------------CHANGE ME-----------------------------------------*/
					label.setOpaque(false);
					
					System.out.println(tag.getName() + " " + tag.getNormScore() + " " + tag.getWeight());
					//label.setFont(label.getFont().deriveFont(tag.getWeight() * 10 < 20? 20f: (float) tag.getScore()*10	));
					label.setFont(label.getFont().deriveFont((float)tag.getWeight()));
					label.setForeground(new Color(random.nextInt(256), random
							.nextInt(256), random.nextInt(256)));

					cloudpanel.add(label);
					label.setVisible(true);

				}
				cloudpanel.setVisible(true);
				frame.add(combopanel, BorderLayout.PAGE_START);
				frame.add(cloudpanel, BorderLayout.CENTER);
				frame.setSize(1000, 800);
				frame.setResizable(false);
				frame.setVisible(true);
			}
		});
		
	
		
		 
		
	}

	public GenerateWordCloud(TagDAO dao) {
		super();
		this.dao = dao;
	}

	public static void invokeWordCloud(final TagDAO dao) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				List<Tag> tags = null;
				new GenerateWordCloud(dao).initUI();
			}
		});
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				List<Tag> tags = null;
				new GenerateWordCloud(null).initUI();
			}
		});
	}

}